package cn.ahaoweb.drule.core.util;

import java.io.*;
import java.net.URL;

/**
 * @Name: DruleUtil
 * @Description:
 * @Author: wangzhonghao
 * @Date: 2025/2/13 19:45
 */
public class DruleUtil {

    public static String getLowerFirstClassName(Class clazz) {
        String name = clazz.getName();
        int i = name.lastIndexOf('.');
        if (i > -1) {
            name = name.substring(i + 1);
        }
        if (name.length() > 0) {
            name = Character.toLowerCase(name.charAt(0)) + name.substring(1);
        }
        return name;
    }

    /**
     * 获取类路径下的文件内容
     * @param path
     * @return
     */
    public static String getFileContentByClassPath(String path) {
        int i = path.lastIndexOf(".");
        String p = path.substring(0, i);
        p = p.replace(".",File.separator);
        path = p + path.substring(i);
        URL systemResource = ClassLoader.getSystemResource(path);
        File file = new File(systemResource.getPath());
        try {
            BufferedReader reader = new BufferedReader(new FileReader(file));
            String s;
            String drl = "";
            while ((s = reader.readLine()) != null) {
                drl += s + "\n";
            }
            return drl;
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
